import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui';
const Login = () => import('@/views/Login/Login')
const Home = () => import('@/views/Home/Home');
const Welcome = () => import('@/views/Home/children/welcome');
const User = () => import('@/components/User/User');
const Right = () => import('@/components/Power/Right')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/Login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: User
      },
      {
        path: '/right',
        component: Right
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由跳转前验证token
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
    return;
  }
  const data = sessionStorage.getItem('token');
  if (!data) {
    Message.error({
      message: '请先登录！'
    })
    next('/login');
    return;
  }
  next()

})
export default router
